<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/vue.js"></script>
    <script src="js/axios.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        #app {
            width: 80%;
            margin: 30px auto;
        }

        .table {
            overflow: hidden;
        }

        .item {
            float: left;
            width: 200px;
            height: 300px;
            border: 1px solid rosybrown;
            margin: 5px;
        }

        .item img {
            width: 200px;
            height: 200px;
        }

        .page button {
            float: left;
            margin-left: 10px;
            width: 100px;
            height: 40px;
        }

        .page .content {
            float: left;
        }

        .page .content .item {
            float: left;
            width: 50px;
            height: 40px;
            background-color: black;
            color: cadetblue;
            text-align: center;
            line-height: 40px;
        }

        .page .content .active {
            background-color: crimson;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="search">
            <div>
                搜索:<input type="text" v-model="key" />
            </div>
            <div>类别：
                <input type="radio" value="" v-model="type" />全部
                <input type="radio" value="1" v-model="type" />服装
                <input type="radio" value="2" v-model="type" />鞋子
                <input type="radio" value="3" v-model="type" />箱包
                <input type="radio" value="4" v-model="type" />美妆
            </div>
            <div>
                排序字段 :<input type="radio" value="id" v-model="sort" />编号
                <input type="radio" value="goodsprice" v-model="sort" />价格
            </div>
            <div>
                排序方式: <input type="radio" value="asc" v-model="order" /> 从小大
                <input type="radio" value="desc" v-model="order" />从大到小
            </div>
            <div>
                价格区间 <input v-model="start" />到<input v-model="end" />
            </div>

        </div>
        <div class="table">
            <div class="item" v-for="item in  list" @click="gotoDetail(item.id)">
                <img :src="item.goodsimg" />
                <p>{{item.goodsname}}</p>
                <p>商品价格{{item.goodsprice}} 商品数量{{item.goodsnum}}</p>
            </div>

        </div>
        <div class="page">

            <button @click="prev" v-if="page>1">上一页</button>
            <button disabled v-else>上一页</button>

            <div class="content">
                <div class="item" v-for="item in pageList" :class="{active:page==item}" @click="setpage(item)">
                    {{item}}
                </div>
            </div>

            <button @click="next" v-if="page<maxPage">下一页</button>
            <button disabled v-else>下一页</button>
        </div>

    </div>

</body>
<script>
    new Vue({
        el: "#app",
        data: {
            order: "asc",//排序方式
            sort: "id",//排序字段
            key: "",//搜索的关键字
            type: "",//商品的类别
            list: [],//商品列表
            start: "",//开始
            end: "",//结束
            limit: 3,//每页显示三条数据
            page: 1,//默认显示第一页
            total: 100,//默认有100条数据
            maxPage: 34,//默认34页
            showpage: 7,//表示连续出现的页码数为5
            pageList: [1, 2, 3, 4, 5],//页码的集合

        },
        watch: {
            key() {//搜索的关键字发生改变的时候触发
                this.page =1;
                this.search();
            },
            type() {//当type发生改变的时候 就触发
                //查询
                this.page =1;
                this.search();
            },
            order() {//排序方式改变了 重新请求
                this.search();
            },
            sort() {//排序字段改变了 重新请求
                this.search();
            },
            start() {
                this.page=1;
                this.search();
            },
            end() {
                this.page=1;
                this.search();
            },
            page() {
                this.search();
            }
        },
        methods: {
            gotoDetail(id){
                window.location.href ="13detail.html?id="+id;
            },
            setpage(page){
                this.page = page*1;
            },
            prev() {
                this.page--;
            },
            next() {
                this.page++;
            },
            async search() {
                //有个bug  点全部的时候 type为 ""
                let params = {
                    q: this.key,//q:query  会查询 所有的字段里面 如果包含了key的值 都会返回
                    _sort: this.sort,
                    _order: this.order,
                    _limit: this.limit,
                    _page: this.page
                };
                if (this.type) {
                    params.type = this.type
                }

                if (this.start) { //需要查询价格 比start大的内容
                    params.goodsprice_gte = this.start * 1;
                }
                if (this.end) {
                    params.goodsprice_lte = this.end * 1;
                }

                let { data: list } = await axios({
                    method: "get", url: "http://localhost:2345/goodsinfos", params
                })
                //上面带了分页的参数

                delete params._limit;
                delete params._page;

                let { data: list2 } = await axios({
                    method: "get",
                    url: "http://localhost:2345/goodsinfos",
                    params
                })
                this.total = list2.length;
                this.maxPage = Math.ceil(this.total / this.limit); //计算总的页码数

                let pageStart = 1;
                let pageEnd = 5;
                let middleNum = Math.ceil(this.showpage / 2);//中间数

                pageEnd = this.maxPage > this.showpage ? this.showpage : this.maxPage;
                //判断一下

                if(this.page>= middleNum){//page 8    中间的判断
                    pageStart = this.page - middleNum+1;// 8-3+1//6
                    pageEnd = this.page +middleNum-1;//8+3-1//10

                }

                if(this.page>this.maxPage-middleNum){//9 -3  到达了末尾
                    pageStart = this.maxPage -this.showpage+1;//9 - 5 +1
                    pageEnd  = this.maxPage;
                }

                pageStart = pageStart<1?1:pageStart;

                this.pageList.length = 0;//将page的列表清空

                for (let i = pageStart; i <= pageEnd; i++) {
                    this.pageList.push(i);
                }

                




                this.list = list;
            }
        },
        async created() {//页面加载的时候 请求所有数据
            this.search();
        }
    })

</script>

</html>